﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CucumberJwt.Application.Authorization.Jwt.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.Controllers
{
    /// <summary>
    /// 令牌
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    
    public class TokenController :  Controller
    {

        private readonly IJwtAppService _jwtAppService = null;

        public TokenController(IJwtAppService jwtAppService)
        {
            _jwtAppService = jwtAppService;
        }


        /// <summary>
        ///创建令牌
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        async public  Task<IActionResult>  CreateToken()
        {
            var jwtAuthenticateDto = await Task.Run(()=> _jwtAppService.CreateToken());
            return new JsonResult(new
            {
                access_token = jwtAuthenticateDto.Token,
                token_type = "Bearer",
                profile = new
                {
                    sid = jwtAuthenticateDto.Sid,

                    auth_time = jwtAuthenticateDto.AuthTime,
                    expires_at = jwtAuthenticateDto.ExpiresAt,
                }
            });
        }
    }
}